<?php
// application/controllers/IndexController.php

class IndexController extends Zend_Controller_Action 
{
    public function indexAction() 
    {
		$flag = $this->_getParam('flag', 0);
		if ( !$flag )
		{
			$this->view->content = 'Hello, This is a petstore build with Zend Framework MVC + Smarty !';
		}
		else 
		{
			$this->view->content = 'oops! no pet found.';
			$this->render('nores');
		}
    }

	public function aboutAction()
	{
		$this->view->content = 'This is IndexController::aboutAction()!';
	}

	public function phpinfoAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		phpinfo();
	}

	public function listAction()
	{
		$this->view->content = "no pet found.";
	}

	public function apiAction()
	{
		$this->_helper->viewRenderer->setNoRender();

		$data = array (
			'id' => '1',
			'name' => 'Kungfu Panda',
			'intro' => "It's a panda be able to perform Chinese kungfu, Aka KungFu Panda! ");
		
		echo json_encode($data);
	}

	public function jsonAction()
	{
		$data = array (
			'id' => '1',
			'name' => 'Kungfu Panda',
			'intro' => "It's a panda be able to perform Chinese kungfu, Aka KungFu Panda! ");

		$this->_helper->json($data);
	}

	private function getMyForm()
	{
		$form = new Zend_Form();
		$form->setAction('/myform')
			->setMethod('post');

		// Create and configure username element:
		$username = $form->createElement('text', 'username');
		$username->addValidator('alnum')
			->addValidator('regex', false, array('/^[a-z]+/'))
			->addValidator('stringLength', false, array(6, 20))
			->setRequired(true)
			->addFilter('StringToLower');

		// Create and configure password element:
		$password = $form->createElement('password', 'password');
		$password->addValidator('StringLength', false, array(6))
			->setRequired(true);

		// Add elements to form:
		$form->addElement($username)
			->addElement($password)
			// use addElement() as a factory to create 'Login' button:
			->addElement('submit', 'login', array('label' => 'Login'));
		return $form;
	}
	
	public function myformAction()
	{

		$form = $this->getMyForm();
		
		if ( $this->getRequest()->isPost() ) {
			if ($form->isValid($_POST)) {
				echo 'ok!';
				// success!
			} else {
				echo 'err!';
				// failure!
			}
			echo '<pre>';
			$values = $form->getValues();
			print_r($values);
			$errors = $form->getErrors();
			print_r($errors);
			$messages = $form->getMessages();
			print_r($messages);
		} else {
			$this->view->form = $form;
		}
	}
}
